/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Windows;

namespace Xceed.Silverlight.ListBox.PositioningFunction
{
  internal class VerticalPositioning : PositioningFunctionBase
  {
    #region Constructors

    internal VerticalPositioning()
    {
    }

    #endregion

    #region DisplayName Property

    internal override string DisplayName
    {
      get
      {
        return "Vertical Positioning function";
      }
    }

    #endregion

    #region IsScrollOffsetDifferenceBetweenPositionsSupported Property

    internal override bool IsScrollOffsetDifferenceBetweenPositionsSupported
    {
      get
      {
        return true;
      }
    }

    #endregion

    #region Internal Methods
    internal override void PrepareLayoutPass( double partialOffset, Size itemContainerSize, Size groupContainerSize )
    {
      m_firstContainerLayouted = false;
      m_cachedPartialOffset = partialOffset;
      m_cachedItemContainerSize = itemContainerSize;
      m_cachedGroupContainerSize = groupContainerSize;
    }

    internal override void PlaceContainerForward( Size container, 
      bool isGroupContainer,
      out Point containerPosition, 
      out double containerProportionInViewport,
      out int layoutProgressionPercentage,
      out double rotateAngle )
    {
      double containerHeight = container.Height;

      if( !m_firstContainerLayouted )
      {
        double pixelsPartialOffset = 0.0;
        // The cached partial offset represents an item container percentage.
        // We need to compute the partial offset in pixels
        if ( isGroupContainer ) 
        {
          pixelsPartialOffset = m_cachedPartialOffset * ( container.Height + m_cachedItemContainerSize.Height );
        }
        else
        {
          // In the case of an item, we just multiply by the item's height.
          pixelsPartialOffset = m_cachedPartialOffset * container.Height;
        }

        // Move the position index to their starting position.
        this.IndexPositionTopBottom = this.StartOffsetIndex - pixelsPartialOffset;
        this.IndexPositionBottomUp = this.StartOffsetIndex - pixelsPartialOffset;
        m_firstContainerLayouted = true;
      }

      layoutProgressionPercentage = 100;
      rotateAngle = 0.0d;
      containerPosition = new Point();
      containerPosition.X = 0.0d;
      containerPosition.Y = this.IndexPositionTopBottom;
      this.IndexPositionTopBottom += containerHeight;
      containerProportionInViewport = this.GetContainerProportionInEffectiveViewport( container, containerPosition );

    }

    internal override void PlaceContainerBackward( Size container,
      bool isGroupContainer,
      out Point containerPosition,
      out double containerProportionInViewport,
      out int layoutProgressionPercentage,
      out double rotateAngle )
    {
      double containerHeight = container.Height;

      this.IndexPositionBottomUp -= containerHeight;
      layoutProgressionPercentage = 100;
      rotateAngle = 0.0d;
      containerPosition = new Point();
      containerPosition.X = 0.0d;
      containerPosition.Y = this.IndexPositionBottomUp;
      containerProportionInViewport = this.GetContainerProportionInEffectiveViewport( container, containerPosition );

    }

    internal override Point GetBringIntoViewPositionForContainer( Size groupHeaderContainerSize, 
      Size itemContainerSize,
      Size viewportSize, 
      bool forwardScrolling )
    {
      double positionY = 0.0d;

      if( !forwardScrolling )
      {
        // If we are not forwardScrolling, we are "scrolling up".  This means
        // we have to position the container just after the sticky header.
        positionY = groupHeaderContainerSize.Height;
      }
      else
      {
        // If we are forward scrolling, we need to make the container
        // fully visible at the end of the viewport
        positionY = viewportSize.Height - ( itemContainerSize.Height );
      }

      // container's X position for the vertical positionning is always 0.
      return new Point( 0.0d, positionY );
    }

    internal override double GetScrollOffsetDifferenceBetweenPositions( Point originalPosition, Point targetPosition, Size containerSize )
    {
      double differenceInPixel = originalPosition.Y - targetPosition.Y;
      double differenceInScrollOffset = differenceInPixel / containerSize.Height;

      return differenceInScrollOffset;
    }

    internal override Rect GetCorrectedViewportVisibilityRect( Size stickyHeaderSize, Rect viewportVisibilityRect )
    {
      // In the case of vetical positionning, we have to exclude
      // the sticky header from the viewport visibility rectangle because
      // containers under the sticky header are not visible.
      viewportVisibilityRect.Y += stickyHeaderSize.Height;
      viewportVisibilityRect.Height -= stickyHeaderSize.Height;

      return viewportVisibilityRect;
    }

    internal override void UpdateEffectiveViewportSizeInPixels( Size newViewportSizeInPixels )
    {
      this.EffectiveViewportStart = 0.0d;
      this.EffectiveViewportEnd = newViewportSizeInPixels.Height;
      double viewportOversizedHeight = newViewportSizeInPixels.Height * 1.50;
      double oversizedInPixels = viewportOversizedHeight - newViewportSizeInPixels.Height;
      double nbPixelsToAddEachSide = oversizedInPixels / 2.0d;
      this.ExtendedViewportStart = this.EffectiveViewportStart - nbPixelsToAddEachSide;
      this.ExtendedViewportEnd = this.EffectiveViewportEnd + nbPixelsToAddEachSide;
      this.StartOffsetIndex = this.EffectiveViewportStart;

      this.ResetPositioningIndexes();
    }

    internal override void CorrectViewportSize( double itemsInViewport, ref Size viewport )
    {
      viewport.Height = itemsInViewport;
    }

    internal override Point PlaceStickyHeader( Point lastContainerOfTheGroupPosition, Size containerSize, out double angle )
    {
      Point stickyPostion = new Point( 0.0d, 0.0d );
      angle = 0.0d;
      stickyPostion.X = lastContainerOfTheGroupPosition.X;
      stickyPostion.Y = lastContainerOfTheGroupPosition.Y + m_cachedItemContainerSize.Height - containerSize.Height;
      stickyPostion.Y = Math.Min( 0.0d, stickyPostion.Y );
      return stickyPostion;
    }

    internal override double GetGroupHeaderProportion()
    {
      return m_cachedGroupContainerSize.Height / ( m_cachedGroupContainerSize.Height + m_cachedItemContainerSize.Height );
    }

    internal override Size GetCorrectedContainerSize( Size containerSize, Size viewportSize )
    {
      return new Size( Math.Min( viewportSize.Width, containerSize.Width ), containerSize.Height );
    }
    #endregion

    #region Protected Internal Methods
    protected internal override double GetContainerProportionInEffectiveViewport( Size containerSize, Point containerPosition )
    {
      // 5 possible cases 
      // #1 container is completly out of the effective viewport (at the top) (0.0)
      // #2 container is completly out of the effective viewport (at the end) (0.0)
      // #3 container is completly in the effective viewport (1.0)
      // #4 container is partially in the effective viewport (at the top) (between 0.0 and 1.0)
      // #5 container is partially in the effective viewport (at the end) (between 0.0 and 1.0)
      double containerHeight = containerSize.Height;
      double containerStartPosition = containerPosition.Y;
      double containerEndPosition = containerPosition.Y + containerHeight;
      double sizeOfContainerInViewport = 0.0d;

      if( containerStartPosition < this.EffectiveViewportStart )
      {
        // Case #1 or #4
        if( containerEndPosition < this.EffectiveViewportStart )
        {
          // Case #1
          return 0.0d;
        }
        else
        {
          // Case #4
          sizeOfContainerInViewport = containerEndPosition - this.EffectiveViewportStart;
          return sizeOfContainerInViewport / containerHeight;
        }
      }
      else if( containerEndPosition > this.EffectiveViewportEnd )
      {
        // Case #2 or #5
        if( containerStartPosition > this.EffectiveViewportEnd )
        {
          // Case #2
          return 0.0d;
        }
        else
        {
          // Case #5
          sizeOfContainerInViewport = this.EffectiveViewportEnd - containerStartPosition;
          return sizeOfContainerInViewport / containerHeight;
        }
      }
      else
      {
        // Case 3
        return 1.0;
      }
    }
    #endregion

    #region Private Members
    private Size m_cachedGroupContainerSize = new Size( 0.0d, 0.0d );
    private Size m_cachedItemContainerSize = new Size( 0.0d, 0.0d );
    private bool m_firstContainerLayouted = false;
    private double m_cachedPartialOffset = 0.0d;
    #endregion
  }
}
